/*     */ package com.yizhilu.os.image.util;
/*     */ 
/*     */ import com.yizhilu.os.core.util.PropertyUtil;
/*     */ import java.awt.Graphics;
/*     */ import java.awt.Image;
/*     */ import java.awt.Toolkit;
/*     */ import java.awt.image.BufferedImage;
/*     */ import java.awt.image.CropImageFilter;
/*     */ import java.awt.image.FilteredImageSource;
/*     */ import java.awt.image.ImageFilter;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import javax.imageio.ImageIO;
/*     */ import org.icepdf.core.pobjects.Document;
/*     */ import org.icepdf.core.pobjects.PDimension;
/*     */ import org.icepdf.core.pobjects.Page;
/*     */ import org.icepdf.core.pobjects.PageTree;
/*     */ 
/*     */ public class PDFUtils
/*     */ {
/*  29 */   public static PropertyUtil propertyUtil = PropertyUtil.getInstance("project");
/*     */ 
/*  38 */   float scale = 2.0F;
/*  39 */   float rotation = 0.0F;
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/*     */     try
/*     */     {
/*  33 */       pdfToPng("E:\\work\\demo_yizhilu_v2.6\\web\\src\\main\\webapp/upload/eduplat/courseKpoint/20160120/1453288686617277283.pdf", "100", "100");
/*     */     } catch (Exception e) {
/*  35 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public static Map<String, String> pdfToPng(String pdfurl, String width, String height)
/*     */     throws Exception
/*     */   {
/*  43 */     Map map = new HashMap();
/*     */ 
/*  45 */     String pngUrlStrs = "";
/*     */ 
/*  47 */     String pngUrlStrsTB = "";
/*  48 */     String pngFileUrl = pdfurl.substring(0, pdfurl.lastIndexOf("."));
/*     */ 
/*  50 */     String pngFileName = pngFileUrl.substring(pngFileUrl.lastIndexOf("/"), pngFileUrl.length());
/*     */ 
/*  52 */     createPath(pngFileUrl);
/*  53 */     Document document = new MyDocument();
/*  54 */     document.setFile(pdfurl);
/*  55 */     int pages = document.getNumberOfPages();
/*     */ 
/*  57 */     for (int i = 0; i < pages; i++) {
/*  58 */       float rotation = 0.0F;
/*  59 */       tranferPer(document, rotation, pngFileUrl + "" + pngFileName + "_pdf_" + i + ".png", i);
/*  60 */       if (i == 0) {
/*  61 */         pngUrlStrs = (pngFileUrl + "" + pngFileName + "_pdf_" + i + ".png").replace(FileUtil.rootpath, "/");
/*  62 */         pngUrlStrsTB = zoomImage((pngFileUrl + "" + pngFileName + "_pdf_" + i + ".png").replace(FileUtil.rootpath, "/"), Integer.valueOf(width).intValue(), Integer.valueOf(height).intValue());
/*     */       } else {
/*  64 */         pngUrlStrs = pngUrlStrs + "," + new StringBuilder().append(pngFileUrl).append("").append(pngFileName).append("_pdf_").append(i).append(".png").toString().replace(FileUtil.rootpath, "/");
/*  65 */         pngUrlStrsTB = pngUrlStrsTB + "," + zoomImage(new StringBuilder().append(pngFileUrl).append("").append(pngFileName).append("_pdf_").append(i).append(".png").toString().replace(FileUtil.rootpath, "/"), Integer.valueOf(width).intValue(), Integer.valueOf(height).intValue());
/*     */       }
/*     */     }
/*  68 */     document.dispose();
/*  69 */     map.put("pngUrlStrs", pngUrlStrs);
/*  70 */     map.put("pngUrlStrsTB", pngUrlStrsTB);
/*  71 */     return map;
/*     */   }
/*     */ 
/*     */   public static void tranferPer(Document document, float rotation, String imagepath, int index)
/*     */     throws Exception
/*     */   {
/*  78 */     float scale = 3.0F;
/*  79 */     Page page = document.getPageTree().getPage(index);
/*  80 */     page.init();
/*  81 */     PDimension sz = page.getSize(2, rotation, scale);
/*  82 */     int pageWidth = (int)sz.getWidth();
/*  83 */     int pageHeight = (int)sz.getHeight();
/*  84 */     BufferedImage image = new BufferedImage(pageWidth, pageHeight, 1);
/*     */ 
/*  86 */     Graphics g = image.createGraphics();
/*  87 */     page.paint(g, 2, 2, rotation, scale);
/*     */ 
/*  89 */     g.dispose();
/*     */     try {
/*  91 */       System.out.println("转换第 " + (index + 1) + " 页");
/*  92 */       File file = new File(imagepath);
/*  93 */       ImageIO.write(image, "jpg", file);
/*     */     } catch (Throwable e) {
/*  95 */       e.printStackTrace();
/*     */     }
/*  97 */     image.flush();
/*     */   }
/*     */ 
/*     */   public static String zoomImage(String srcFileName, int width, int height)
/*     */   {
/* 105 */     String tagFileName = srcFileName.substring(0, srcFileName.lastIndexOf(".")) + "-small" + srcFileName.substring(srcFileName.lastIndexOf("."), srcFileName.length());
/* 106 */     srcFileName = propertyUtil.getProperty("file.root") + srcFileName;
/*     */     try
/*     */     {
/* 110 */       BufferedImage bufferedImage = ImageIO.read(new File(srcFileName));
/*     */ 
/* 113 */       int srcWidth = bufferedImage.getWidth();
/* 114 */       int srcHeight = bufferedImage.getHeight();
/* 115 */       float _scale = Float.valueOf(width).floatValue() / srcWidth;
/* 116 */       srcHeight = (int)(Float.valueOf(srcHeight).floatValue() * _scale);
/*     */ 
/* 119 */       Image image = bufferedImage.getScaledInstance(width, srcHeight, 4);
/*     */ 
/* 121 */       ImageFilter cropFilter = new CropImageFilter(0, 0, width, height);
/* 122 */       Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
/*     */ 
/* 124 */       BufferedImage outputImage = new BufferedImage(width, height, 1);
/* 125 */       Graphics g = outputImage.getGraphics();
/* 126 */       g.drawImage(img, 0, 0, width, height, null);
/* 127 */       g.dispose();
/*     */ 
/* 130 */       ImageIO.write(outputImage, getSuffix(propertyUtil.getProperty("file.root") + tagFileName), new File(propertyUtil.getProperty("file.root") + tagFileName));
/*     */     }
/*     */     catch (IOException e) {
/* 133 */       e.printStackTrace();
/*     */     }
/* 135 */     return tagFileName;
/*     */   }
/*     */ 
/*     */   public static void createPath(String filePath)
/*     */   {
/* 142 */     filePath = filePath.toString();
/* 143 */     File myFilePath = new File(filePath);
/* 144 */     if (!myFilePath.exists()) myFilePath.mkdirs();
/*     */   }
/*     */ 
/*     */   private static String getSuffix(String str)
/*     */   {
/* 152 */     return str = str.substring(str.lastIndexOf(".") + 1);
/*     */   }
/*     */ }

/* Location:           F:\work\公司\质量学院\ducumentQIN\项目更新\web\20171122\
 * Qualified Name:     com.yizhilu.os.image.util.PDFUtils
 * JD-Core Version:    0.6.1
 */